/*
    openssl.me -- OpenSSL Component for the MPR
 */

Me.load({ 
    targets: {
        openssl: {
            description: 'OpenSSL Support',
            libname: 'libmpr-openssl',
            config: function (target) {
                if (me.options.gen) {
                    let result = {
                        /*
                            Static configuration to work for standard installations under /usr/lib and /usr/include
                            and in a custom user directory.
                         */
                        path: me.dir.bin.join(target.libname).joinExt(me.ext.lib),
                        includes: [ me.dir.inc ],
                        libraries: me.platform.os == 'windows' ? 
                            ([ 'gdi32', 'libeay32', 'ssleay32' ]) : ([ 'ssl', 'crypto' ]),
                    }
                    if (me.options.gen == 'make') {
                        result.defines = ['ME_COM_OPENSSL_PATH=/usr/lib']
                    } else if (me.options.gen == 'nmake') {
                        result.defines = ['ME_COM_OPENSSL_PATH=']
                    }
                    if (me.options.gen == 'make' || me.options.gen == 'nmake' || me.options.gen == 'xcode') {
                        result.includes.push('$(ME_COM_OPENSSL_PATH)/include')
                        result.libpaths = ['$(ME_COM_OPENSSL_PATH)']
                    }
                    return result
                }
                let lib, libraries, libpaths, imports, includes, path
                let search = getComponentSearch(target, 'openssl')
                if (me.platform.os == 'windows') {
                    search = search.map(function(p:Path) p.join('out32')) + 
                             search.map(function(p:Path) p.join('out32dll'))
                    lib = probe('libeay32.lib', {fullpath: true, search: search, nopath: true})
                    path = lib.dirname.parent
                    libpaths =  [ lib.parent ]
                    libraries = [ 'gdi32', 'libeay32', 'ssleay32' ]
                    imports =   [ lib.parent.join('libeay32.lib'), lib.parent.join('ssleay32.lib') ]
                    if (lib.dirname == 'out32dll') {
                        imports += [ lib.parent.join('libeay32.dll'), lib.parent.join('ssleay32.dll') ]
                    }
                    includes = [ probe('openssl/ssl.h', {search: [path.join('inc32')]}) ]

                } else {
                    let opt = {fullpath: true, search: search, nopath: true}
                    lib = probe('libcrypto.' + me.ext.lib, blend({nothrow: true}, opt)) ||
                          probe('libcrypto.' + me.ext.shobj, opt)
                    path = lib.dirname
                    libraries = [ 'ssl', 'crypto' ]
                    libpaths = [ lib.parent ]
                    let ext = lib.extension
                    imports = lib.parent.files('libssl*.' + ext + '*') + lib.parent.files('libcrypto*.' + ext + '*')
                    includes = [ probe('openssl/ssl.h', {search: [ path.join('include'), '/usr/include']}) ]
                }
                includes.push(me.dir.inc)
                return {
                    location:   path,
                    path:       me.dir.bin.join(target.libname).joinExt(me.ext.lib),
                    imports:    imports,
                    includes:   includes,
                    libpaths:   libpaths,
                    libraries,  libraries,
                }
            },
            ifdef:     [ 'ssl' ],
            conflicts: [ 'est', 'matrixssl', 'nanossl' ],
            type: 'lib',
            static: true,
            sources: [ '*.c' ],
            postresolve: `
                if (me.platform.os == 'macosx') {
                    me.target.compiler.push('-Wno-deprecated-declarations')
                }
            `
        }
    }
})

